package 普通数组;

import java.util.Arrays;

/**
 * @author tmh
 * @date 2025/3/7 18:35
 * @description
 */
public class T56合并区间 {
    public int[][] merge(int[][] intervals) {
        int index = 0;
        Arrays.sort(intervals, (num1, num2) -> num1[0] - num2[0]);
        int[][] merge = new int[intervals.length][2];
        merge[0] = intervals[0];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] > merge[index][1]) {
                merge[++index] = intervals[i];
            } else {
                int maxValue = Math.max(merge[index][1], intervals[i][1]);
                merge[index][1] = maxValue;
            }
        }
        return Arrays.copyOf(merge, index + 1);
    }
}
